#pragma once

#include "Common.h"

#define PIN_SWITCH_LEFT A0
#define PIN_SWITCH_RIGHT A1

class EyeSwitches 
{

public:

  inline static void Init() 
  {
    pinMode( PIN_SWITCH_LEFT, INPUT );
    pinMode( PIN_SWITCH_RIGHT, INPUT );
    
    digitalWrite(PIN_SWITCH_LEFT, HIGH);
    digitalWrite(PIN_SWITCH_RIGHT, HIGH);
    
    NextCheckTimeInMs = 0;
  }

  inline static void UpdateNextCheckTime() 
  {
    NextCheckTimeInMs = globals::CurrentLoopTimeInMs + 100;
  }

  inline static bool IsCheckTimeReached() 
  {
    return (NextCheckTimeInMs < globals::CurrentLoopTimeInMs);
  }

  inline static void DoUpdateState() 
  {
    bool new_state;
    
    new_state = digitalRead(PIN_SWITCH_LEFT);
    if ( StateSwitchLeft != new_state ) {
      IsStateChanged = true;
      StateSwitchLeft = new_state;
    }

    new_state = digitalRead(PIN_SWITCH_RIGHT);
    if ( StateSwitchRight != new_state ) {
      IsStateChanged = true;
      StateSwitchRight = new_state;
    }
    
  }

  inline static void UpdateState() 
  {
    if ( IsCheckTimeReached() ) {
      DoUpdateState();
      UpdateNextCheckTime(); 
    }
  }

  static unsigned long NextCheckTimeInMs;  
  static bool StateSwitchLeft;
  static bool StateSwitchRight;
  static bool IsStateChanged;
};


